package org.itboys.product.service;

import com.google.common.collect.Maps;
import org.itboys.commons.CommonConstants;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseService;
import org.itboys.product.entity.ProductSpec;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by huml on 2016/4/8.
 * 产品规格种类业务
 */
@Service
public class ProductSpecService extends BaseService <ProductSpec, Long> {

    private static final long serialVersionUID = -1301133949594243831L;

    @Resource(name="productDS")
    private MongoDataSource productSpecDataSource;

    @Override
    protected MongoDataSource getMongoDataSource() {
        return productSpecDataSource;
    }

    @Override
    protected Class<ProductSpec> getEntityClass() {
        return ProductSpec.class;
    }

    /**
     * 保存/编辑 产品规格种类
     * @param productId
     * @param productCategorySpecIds
     */
    public void insert(Long productId, List<Long> productCategorySpecIds){
        ProductSpec spec = getByField("productId",productId);
        //先看产品有没有规格种类,有的话就更新,没有就插入一条
        if(spec == null){
            spec = new ProductSpec();
            spec.setProductId(productId);
            spec.setProductCategorySpecIds(productCategorySpecIds);
            super.save(spec);
        } else {
            spec.setProductCategorySpecIds(productCategorySpecIds);
            super.update(spec);
        }
    }

    /**
     * 获取产品选取的规格id集合
     * @param productId
     * @return
     */
    public List<Long> getSpecIdByProduct(Long productId){
        ProductSpec productSpec = super.getByField("productId", productId);
        return productSpec == null ? null : productSpec.getProductCategorySpecIds();
    }

}
